from pyecharts.charts import Line
from pyecharts import options as opts
from pyecharts.commons.utils import JsCode

# 用JS画出颜色
gradient_color_js = (
    "new echarts.graphic.LinearGradient(0, 0, 0, 1, "
    "[{offset: 0, color: '#c86589'}, {offset: 1, color: '#06a7ff'}], false)"
)
background_color = "#8995AF"


def DrawChart(H_Tem, L_Tem, date, day):
    # 图表对象
    Chart = Line(init_opts=opts.InitOpts(bg_color=background_color))
    # 增加自变量坐标
    Chart.add_xaxis(xaxis_data=date)

    # 增加一条因变量数据
    Chart.add_yaxis('最高温度',
                    y_axis=H_Tem,
                    symbol="emptyCircle",
                    # 显示线上的圆点
                    is_symbol_show=True,
                    # 平滑过渡
                    is_smooth=True,
                    # True为在曲线上忽略（不连接）空白数据点
                    is_connect_nones=True,
                    # 显示线上的数值
                    label_opts=opts.LabelOpts(is_show=True),
                    # 此处为堆叠区域，线的16进制色号为"#5A6B8F"，区域为"#8995AF"
                    # areastyle_opts=opts.AreaStyleOpts(opacity=1, color="#8995AF"),
                    areastyle_opts=opts.AreaStyleOpts(color=JsCode(gradient_color_js), opacity=0.7)
                    )
    # 增加一条因变量数据
    Chart.add_yaxis('最低温度',
                    y_axis=L_Tem,
                    symbol="emptyCircle",
                    # 显示线上的圆点
                    is_symbol_show=True,
                    # 平滑过渡
                    is_smooth=True,
                    # 显示线上的数值
                    label_opts=opts.LabelOpts(is_show=True),
                    # 此处为堆叠区域，线的16进制色号为"#5A6B8F"，区域为"#8995AF"
                    # areastyle_opts=opts.AreaStyleOpts(opacity=1, color="#8995AF"),
                    areastyle_opts=opts.AreaStyleOpts(color=background_color, opacity=0.4)
                    )
    # 设置图标名称
    Chart.set_global_opts(tooltip_opts=opts.TooltipOpts(is_show=True),
                          title_opts=opts.TitleOpts(title="佛山", subtitle="7天气温走势"),
                          yaxis_opts=opts.AxisOpts(type_="value",
                                                   # True为隐藏线上的圆点及数值
                                                   axistick_opts=opts.AxisTickOpts(is_show=True),
                                                   # True为显示平行于X轴的水平线
                                                   splitline_opts=opts.SplitLineOpts(is_show=True)),
                          # True为X轴的系列名显示在两刻度的中间，False显示在每一刻度之上
                          xaxis_opts=opts.AxisOpts(type_="category", boundary_gap=False))
    return Chart


def Generate(dictinfo):
    # 示例数据
    Date = dictinfo['date']
    Day = dictinfo['day']
    HighlyTemperature = dictinfo['MaximumTemperature']
    LowlyTemperature = dictinfo['MinimumTemperatureDataList']

    ChartObject = DrawChart(HighlyTemperature, LowlyTemperature, Date, Day)
    ChartObject.render("WeatherReport.html")
    return


if __name__ == '__main__':
    Dictinfo = {'UpdateTime': '2020-12-26 20:48:54', 'City': '佛山',
                'MaximumTemperature': ['23', '23', '24', '22', '12', '12', '14'],
                'MinimumTemperatureDataList': ['16', '16', '16', '10', '5', '4', '4'],
                'date': ['2020-12-26', '2020-12-27', '2020-12-28', '2020-12-29', '2020-12-30', '2020-12-31',
                         '2021-01-01'], 'day': ['星期六', '星期日', '星期一', '星期二', '星期三', '星期四', '星期五']}

    Generate(Dictinfo)
"""
#增加平均线
Chart.add_yaxis('最高温度', H_Tem, is_smooth=True,
                    markline_opts=opts.MarkLineOpts(data=[opts.MarkLineItem(type_="average")]))
#增加某节点的标记
Chart.add_yaxis('最低温度', L_Tem, is_smooth=True,
                    markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem(name="自定义标记点",
                                                                               coord=[cate[2], LowlyTemperature[2]],
                                                                               value=LowlyTemperature[2])]))
"""
